<script type="text/javascript">

$(document).ready(function () {
	$('#username').select();
	
	$('#submit_account').click(function(){
		//$("#submit_display").empty();
		//$("#submit_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
		
		var form_data = {
			name: $('#username').val(),
			pass: $('#password').val(),
			pass2: $('#password2').val(),
			email: $('#email').val()
		};
		
		$.ajax({
           url: "<?php echo site_url('account/check_account_form');?>",
		   type: 'POST',
           data: form_data,
           
           success: function (msg) {
				var _text = "error";
				var _color = "red";
				if(msg!="okokok"){
					_text = "Invalid form data";
					_color = "orange";
				}else{
					_text = "Account created";
					_color = "green";

					//switch to the new page
					var _window = $('#submit_account').closest("div");
					close_window(_window);
					var my_window = add_window(300,110,"Account Created!");
					//$(my_window).append(msg);
					//$(_window).delay(500).fadeIn(500);
					$(my_window).append($("<div>").load("<?php echo base_url();?>application/views/insert_account.php"));
					$(my_window).append(form_data['name']);

					
					
					//$(_window).load("<?php echo base_url();?>application/views/insert_account.php",{},function(){
					//	$(this).append(form_data['name']);
		            //});
				}
				$("#submit_display").empty();
				$("#submit_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
				
				
           }
         });
		 
		return false;
	});
	//$('#username').change(function () {
	$('#username').keyup(function () {
		//$("#username_display").empty();
		//$("#username_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
		
		var form_data = {
			name: $('#username').val()
			//email2: $('#email2').val()
		};
		
		$.ajax({
           url: "<?php echo site_url('account/check_username/null');?>",
		   type: 'POST',
           data: form_data,
           
           success: function (msg) {
               
				var _text = "error";
				var _color = "red";
				if(msg=="letter"){
					_text = "Invalid characters";
					_color = "orange";
				}
				else if(msg=="taken"){
					_text = "Name taken";
					_color = "orange";
				}
				else{
					_text = "Ok";
					_color = "green";
				}
				$("#username_display").empty();
				$("#username_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
           }
         });
		 
		return false;
	});
	
	$('#password').keyup(function () {
		//$("#password_display").empty();
		//$("#password_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
		
		var form_data = {
			pass: $('#password').val(),
			pass2: $('#password2').val()
		};
		
		$.ajax({
           url: "<?php echo site_url('account/check_password/null');?>",
		   type: 'POST',
           data: form_data,
           
           success: function (msg) {
				var _text = "error";
				var _color = "red";
				if(msg=="length"){
					_text = "Invalid length";
					_color = "orange";
				}
				else if(msg=="nomatch"){
					_text = "Doesn't match";
					_color = "orange";
					$("#password_display2").empty();
					$("#password_display2").append("<div style='color: "+_color+";'>"+_text+"</div>");
				}
				else{
					_text = "Ok";
					_color = "green";
					$("#password_display2").empty();
					$("#password_display2").append("<div style='color: "+_color+";'>"+_text+"</div>");
				}
				$("#password_display").empty();
				$("#password_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
              //$("#username_display").replaceWith("<div style='color: "+_color+";'>"+_text+"</div>");
           }
         });
		 
		return false;
	});

	$('#password2').keyup(function () {
		//$("#password_display2").empty();
		//$("#password_display2").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
		
		var form_data = {
			pass: $('#password').val(),
			pass2: $('#password2').val()
		};
		
		$.ajax({
           url: "<?php echo site_url('account/check_password/null');?>",
		   type: 'POST',
           data: form_data,
           
           success: function (msg) {
				var _text = "error";
				var _color = "red";
				if(msg=="length"){
					_text = "Invalid length";
					_color = "orange";
				}
				else if(msg=="nomatch"){
					_text = "Doesn't match";
					_color = "orange";
					$("#password_display").empty();
					$("#password_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
				}
				else{
					_text = "Ok";
					_color = "green";
					$("#password_display").empty();
					$("#password_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
				}
				$("#password_display2").empty();
				$("#password_display2").append("<div style='color: "+_color+";'>"+_text+"</div>");
              //$("#username_display").replaceWith("<div style='color: "+_color+";'>"+_text+"</div>");
           }
         });
		 
		return false;
	});
	
	$('#email').keyup(function () {
		//$("#email_display").empty();
		//$("#email_display").append("<img id='theImg' src='<?php echo base_url();?>application/views/images/ajax-loader.gif'/>");
		
		var form_data = {
			email: $('#email').val(),
		};
		
		$.ajax({
           url: "<?php echo site_url('account/check_email/null');?>",
		   type: 'POST',
           data: form_data,
           
           success: function (msg) {
				var _text = "error";
				var _color = "red";
				if(msg=="error"){
					_text = "Invalid emailaddress";
					_color = "orange";
				}else{
					_text = "Ok";
					_color = "green";
				}
				$("#email_display").empty();
				$("#email_display").append("<div style='color: "+_color+";'>"+_text+"</div>");
              //$("#username_display").replaceWith("<div style='color: "+_color+";'>"+_text+"</div>");
           }
         });
		 
		return false;
	});

});

</script>